<?php
/**
 * Main class for module iMessages
 * 
 * @package      WebGuru3
 * @subpackage   modules/imessages/
 * @author       Ondrej Rafaj
 * @author       WebGuruCMS3 Framework CMS admin generator (http://www.webgurucms.com)
 * @version      1.0.0.0
 * @wgversion    3.0.0.0
 * @wgdeveloper  1.0.0.1
 * @since        1. February 2010
 */

class moduleImessages {
	
	public $name            = NULL;
	public $version         = NULL;
	public $author          = NULL;
	
	private static $_path   = NULL;
	private static $_module = NULL;
	
	
	public function __construct() {
		$this->_init();
	}
	
	
	private function _init() {
		$this->name    = 'iMessages';
		$this->code    = 'imessages';
		$this->version = '0.0.0.1';
		$this->author  = 'Ondrej Rafaj';
		
		$this->_module = dbSystem::getModulesByName($this->code);
		$this->_path   = wgPaths::getAdminPath().$this->_module['part'].'/';
		
		
	}
	
	// ------------------------- class functions -------------------------
	
	public function sendForm($formCode, $data) {
		if (empty($formCode)) return false;
		$form = ImessagesFormsModel::getOneCodeData($formCode);
		if (!(bool) $form->getId()) return false;
		$fields = ImessagesFieldsModel::getSelfFormData($form->getId());
		$recipients = ImessagesRecipientsModel::getSelfData($form->getId());
		
		$from = NULL;
		$fromfield = ImessagesFieldsModel::getEmailFieldForForm($form->getId());
		if (isset($data[$fromfield])) $from = $data[$fromfield];
		if (empty($from)) $from = 'imessage@xprogress.com';
		
		$subject = ''.$form->getName().'';
		
		$html = '<h3>'.$form->getName().'</h3><p>&nbsp;</p>';
		//$html .= '<p><strong>Link</strong>: <a href="http://www.xprogress.com/'.$identifier.'-'.$id.'-link/">http://www.xprogress.com/'.$identifier.'-'.$id.'-link/</a></p>';
		$todb = array();
		foreach ($fields as $field) {
			if (!isset($data[$field->getFcode()])) {
				$data[$field->getFcode()] = NULL;
			}
			$html .= '<p><strong>'.$field->getName().'</strong>: '.$data[$field->getFcode()].'</p>';
			$todb[$field->getName()] = $data[$field->getFcode()];
		}
		
		$save = array();
		$save['imessages_forms_id'] = $form->getId();
		$save['data'] = xml::arrayToXml($todb);
		$save['from'] = $from;
		$save['added'] = 'NOW()';
		$save['device'] = wgGet::getValue('device');
		$save['code'] = wgGet::getValue('code');
		ImessagesMessagesModel::doInsert($save);
		
		$html .= '<p>&nbsp;</p><p><small>This email has been generated by iMessage forms tool on <a href="http://www.xprogress.com/?ref=mail&amp;id='.$form->getId().'">xProgress.com</a></small></p>';
		
		//include(wgPaths::getAdminPath().'config/config.php');
		$conf = wgConfig::getSystemConfiguration();
		$smtp = array();
		$smtp['host'] = (isset($conf['smtp']['host'])) ? $conf['smtp']['host'] : NULL;
		$smtp['auth'] = (isset($conf['smtp']['auth'])) ? (bool) $conf['smtp']['auth'] : false;
		if ((bool) $smtp['auth']) {
			$smtp['username'] = (isset($conf['smtp']['name'])) ? $conf['smtp']['name'] : NULL;
			$smtp['password'] = (isset($conf['smtp']['pass'])) ? $conf['smtp']['pass'] : NULL;
		}
		
		$errors = array();
		
		require_once('Mail.php');
		require_once('Mail/mime.php');
		
		$message = new Mail_mime();
		$message->setHTMLBody($html);
		
		$body = $message->get();
		$extraheaders = array('From'=>$from, 'Subject'=>$subject);
		$headers = $message->headers($extraheaders);
		
		if ((bool) $smtp['host']) $mail = Mail::factory('smtp', $smtp);
		else $mail = Mail::factory('mail');
		
		$res = array();
		
		if (!empty($recipients)) foreach ($recipients as $r) {
			$res['result'] = $mail->send($r->getMail(), $headers, $body);
			if (PEAR::isError($res)) $res['error'] = $res->getMessage();
			else $res['error'] = '';
		}
		
		return $res;
	}
}
		
?>